package com.example.echoewish.ui.my_activity.other_comment_activity;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

import com.example.echoewish.R;
import com.example.echoewish.pojo.OthersComment;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.ui.EchoWishCompatActivity;

import java.util.LinkedList;
import java.util.List;

public class OthersCommentActivity extends EchoWishCompatActivity {


    private ListView othersCommentListView;
    private OthersCommentAdapter othersCommentAdapter;

    private OtherCommentViewModel otherCommentViewModel;


    private Handler othersCommentHandler =new Handler()
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE:
                    if(msg.arg1==OtherCommentViewModel.CHANGE_LIST_VIEW)
                    {
                        othersCommentAdapter.clear();
                        othersCommentAdapter.addAll(otherCommentViewModel.othersCommentMutableLiveData.getValue());
                        othersCommentAdapter.notifyDataSetChanged();
                    }
                    break;
                case OtherCommentViewModel.DELETE_FAIL:
                    Toast.makeText(OthersCommentActivity.this,"连接失败",Toast.LENGTH_SHORT).show();

                    break;
                case OtherCommentViewModel.DELETE_SUCCESS:
                    Toast.makeText(OthersCommentActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    othersCommentAdapter.remove(otherCommentViewModel.deleteCommentMutableLiveData.getValue());
                    othersCommentAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        othersCommentAdapter.setButtonClickListener(new OthersCommentAdapter.ButtonClickListener() {
            @Override
            public void buttonClickListener(int nowPosition) {
                System.out.println(nowPosition);
                otherCommentViewModel.deleteCommentMutableLiveData.setValue(othersCommentAdapter.getItem(nowPosition));
            }
        });
        otherCommentViewModel.getOtherComment();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_others_comment);
    }

    public void initView()
    {
        othersCommentListView=findViewById(R.id.others_comment_list_view);
        othersCommentAdapter=new OthersCommentAdapter(this,R.layout.others_comment_item,new LinkedList<>());
        othersCommentListView.setAdapter(othersCommentAdapter);
    }

    @Override
    public void initViewModel() {
        otherCommentViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(OtherCommentViewModel.class);
    }

    @Override
    public void observeLiveData() {
        otherCommentViewModel.othersCommentMutableLiveData.observe(this, new Observer<List<OthersComment>>() {
            @Override
            public void onChanged(List<OthersComment> othersComments) {
                if(othersComments!=null)
                {
                    Message message=new Message();
                    message.what= Constant.HTTPS_GET_CALLBACK_SUCCESS_MESSAGE;
                    message.arg1=OtherCommentViewModel.CHANGE_LIST_VIEW;
                    othersCommentHandler.sendMessage(message);
                }
                else
                {
                    Message message=new Message();
                    message.what= Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE;
                }
            }
        });

        otherCommentViewModel.deleteCommentMutableLiveData.observe(this, new Observer<OthersComment>() {
            @Override
            public void onChanged(OthersComment othersComment) {
                otherCommentViewModel.deleteOtherComment();
            }
        });

        otherCommentViewModel.responseMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Message message=new Message();
                if (s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                {
                    message.what=OtherCommentViewModel.DELETE_SUCCESS;
                    othersCommentHandler.sendMessage(message);
                }
                else
                {
                    message.what=OtherCommentViewModel.DELETE_FAIL;
                    othersCommentHandler.sendMessage(message);
                }
            }
        });
    }
}